package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Notice;
import com.powernode.dto.NoticeDto;
import com.powernode.service.impl.NoticeService;
import com.powernode.utils.WebMVCUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    //分页查询公告数据
    @RequestMapping("list.do")
    @ResponseBody
    public Result list(NoticeDto noticeDto) {
        System.out.println(noticeDto.getPage()+"---"+noticeDto.getLimit());

        PageInfo<Notice> pageInfo = noticeService.queryNoticeForPage(noticeDto);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }


    //添加公告
    @PostMapping("addNotice.do")
    @ResponseBody
    public Result addNotice(Notice notice) {
        System.out.println("我是添加公告----"+notice);
        try {
            //设置创建更新世间
            notice.setCreateTime(new Date());
            int i = this.noticeService.insertSelective(notice);
            //保存
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    //修改
    @PostMapping("updateNotice.do")
    @ResponseBody
    public Result updateNotice(Notice notice) {
        try {
              notice.setCreateTime(new Date());
            //保存
            this.noticeService.updateByPrimaryKeySelective(notice);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {

            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }


    //公告删除
    @ResponseBody
    @RequestMapping("deleteNoticeByid.do")
    public Result deleteNoticeByid(Integer id) {
        try {
            this.noticeService.deleteByPrimaryKey(id);
            return Result.DEL_SUCCESS;
        } catch (Exception e) {
            return Result.DEL_ERROR;
        }
    }


}
